<template>
	<view>
		<my-search @click="gotoSearch"></my-search>
		<view class="scroll-view-container">
			<!-- 左边滑动区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<block v-for="(leftItem, i) in categoryList" :key="i">
					<view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChange(i)">
						{{leftItem.cat_name}}</view>
				</block>
			</scroll-view>
			<!-- 右边滑动区域 -->
			<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="rightScrollTop">
				<view class="category-l2" v-for="(rightItem, j) in categoryL2List" :key="j">
					<view class="category-l2-title">
						/ {{rightItem.cat_name}} /
					</view>
					<view class="category-l3">
						<view class="category-l3-item" v-for="(rightInItem, k) in rightItem.children" :key="k"
							@click="gotoGoodsList(rightInItem)">
							<image :src="rightInItem.cat_icon"></image>
							<text>{{rightInItem.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		data() {
			return {
				wh: 0,
				categoryList: [],
				categoryL2List: [],
				active: 0,
				rightScrollTop: 0
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 50
			this.getCategoryList()
		},
		methods: {
			async getCategoryList() {
				const {
					data: res
				} = await uni.$http.get('/categories')
				if (res.meta.status != 200) return uni.$showMsg()
				this.categoryList = res.message
				this.categoryL2List = this.categoryList[0].children
			},
			activeChange(index) {
				this.active = index
				this.categoryL2List = this.categoryList[index].children
				this.rightScrollTop = this.rightScrollTop ? 0 : 1
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		},
    mixins: [badgeMix],
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				background-color: #F7F7F7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;

				&.active {
					background-color: #FFFFFF;
					position: relative;

					&::before {
						content: '';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #C00000;
						position: absolute;
						top: 50%;
						left: 0;
						transform: translateY(-50%);
					}
				}
			}
		}

		.category-l2-title {
			font-size: 12px;
			font-weight: bold;
			text-align: center;
			padding: 15px 0;
		}

		.category-l3 {
			display: flex;
			flex-wrap: wrap;

			.category-l3-item {
				width: 33.33%;
				margin-bottom: 10px;
				display: flex;
				flex-direction: column;
				align-items: center;

				image {
					width: 60px;
					height: 60px;
				}

				text {
					font-size: 12px;
				}
			}
		}
	}
</style>